package kt.Utils

import sample.bean.Point
import java.util.*

/**
 * 四个顶点
 */
fun getRegionMax(pointsList: MutableList<Point>): List<Point> {
    val resultList = arrayListOf<Point>()

    pointsList.sortBy { it.x }
    resultList.add(pointsList[0])
    resultList.add(pointsList[pointsList.size - 1])

    pointsList.sortBy { it.y }
    resultList.add(pointsList[0])
    resultList.add(pointsList[pointsList.size - 1])

    return resultList
}

/**
 * 对角线
 */
fun getReginMax2(pointsList: MutableList<Point>): List<Point> {
    val resultList = arrayListOf<Point>()

    pointsList.sortBy { it.x }
    val xMin = pointsList[0].x
    val xMax = pointsList[pointsList.size - 1].x

    pointsList.sortBy { it.y }
    val yMin = pointsList[0].y
    val yMax = pointsList[pointsList.size - 1].y

    resultList.add(Point(xMin, yMin))
    resultList.add(Point(xMax, yMax))

    return resultList
}

/**
 * 影像行列号计算
 */
fun getRegionColRow(region: List<Point>, zoom: Int): ArrayList<Int> {
    val xMin = region[0].x
    val ymin = region[0].y
    val xMax = region[1].x
    val yMax = region[1].y

    val colMin: Int = ((xMin - (-180)) / (0.7031249999891485 / (Math.pow(2.0, (zoom - 0.0)))) / 256).toInt()
    val rowMin: Int = (-1 * (yMax - 90) / (0.7031249999891485 / (Math.pow(2.0, (zoom - 0.0)))) / 256).toInt()

    val colMax: Int = ((xMax - (-180)) / (0.7031249999891485 / (Math.pow(2.0, (zoom - 0.0)))) / 256).toInt()
    val rowMax: Int = (-1 * (ymin - 90) / (0.7031249999891485 / (Math.pow(2.0, (zoom - 0.0)))) / 256).toInt()

//    Log.e("zhangchen", "colMin = " + colMin + ", rowMin = " + rowMin)
//    Log.e("zhangchen", "colMax = " + colMax + ", rowMax = " + rowMax)

    val result = arrayListOf(colMin, rowMin, colMax, rowMax)

    return result
}

/**
 * 天地图行列号计算
 */
fun getRegionColRowTianDi(region: List<Point>, zoom: Int): ArrayList<Int> {
    val xMin = region[0].x
    val ymin = region[0].y
    val xMax = region[1].x
    val yMax = region[1].y

    val colMin: Int = ((xMin - (-180)) / (0.7031249999891485 / (Math.pow(2.0, (zoom - 1.0)))) / 256).toInt()
    val rowMin: Int = (-1 * (yMax - 90) / (0.7031249999891485 / (Math.pow(2.0, (zoom - 1.0)))) / 256).toInt()

    val colMax: Int = ((xMax - (-180)) / (0.7031249999891485 / (Math.pow(2.0, (zoom - 1.0)))) / 256).toInt()
    val rowMax: Int = (-1 * (ymin - 90) / (0.7031249999891485 / (Math.pow(2.0, (zoom - 1.0)))) / 256).toInt()

//    Log.e("zhangchen", "colMin = " + colMin + ", rowMin = " + rowMin)
//    Log.e("zhangchen", "colMax = " + colMax + ", rowMax = " + rowMax)

    val result = arrayListOf(colMin, rowMin, colMax, rowMax)

    return result
}

fun getRegionColRowForest(region: List<Point>, zoom: Int): ArrayList<Int> {
    val xMin = region[0].x
    val ymin = region[0].y
    val xMax = region[1].x
    val yMax = region[1].y

    val colMin: Int = ((xMin - (-0)) / (0.7031249999891485 / (Math.pow(2.0, (zoom + 1.0)))) / 256).toInt()
    val rowMin: Int = (-1 * (yMax - 90) / (0.7031249999891485 / (Math.pow(2.0, (zoom + 1.0)))) / 256).toInt()

    val colMax: Int = ((xMax - (-0)) / (0.7031249999891485 / (Math.pow(2.0, (zoom + 1.0)))) / 256).toInt()
    val rowMax: Int = (-1 * (ymin - 90) / (0.7031249999891485 / (Math.pow(2.0, (zoom + 1.0)))) / 256).toInt()

//    Log.e("zhangchen", "colMin = " + colMin + ", rowMin = " + rowMin)
//    Log.e("zhangchen", "colMax = " + colMax + ", rowMax = " + rowMax)

    val result = arrayListOf(colMin, rowMin, colMax, rowMax)

    return result
}

fun getRegionColRowByGoogle(region: List<Point>, zoom: Int) : ArrayList<Int> {
    val xMin = region[0].x
    val ymin = region[0].y
    val xMax = region[1].x
    val yMax = region[1].y

    val colRowMin: IntArray = googleLonLatToXYZ(xMin, ymin, zoom)
    val colRowMax: IntArray = googleLonLatToXYZ(xMax, yMax, zoom)

    //谷歌切片与天地图切片规则不同，故最大最小行列号计算规则不停，调整顺序
    return arrayListOf(colRowMin[0], colRowMax[1], colRowMax[0], colRowMin[1])
}

/**
 * 谷歌下转换经纬度对应的层行列
 *
 * @param lon  经度
 * @param lat  维度
 * @param zoom 在第zoom层进行转换
 * @return
 */
fun googleLonLatToXYZ(lon: Double, lat: Double, zoom: Int): IntArray {
    val n = Math.pow(2.0, zoom.toDouble())
    val tileX = (lon + 180) / 360 * n
    val tileY = (1 - Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat))) / Math.PI) / 2 * n
    val xy = IntArray(2)
    xy[0] = Math.floor(tileX).toInt()
    xy[1] = Math.floor(tileY).toInt()
    return xy
}


fun getRegionMAllLvByGoogle(region: List<Point>, zoom: Int): MutableList<ArrayList<Int>> {
    val result = mutableListOf<ArrayList<Int>>()
    for (i in 1..zoom) {
        result.add(getRegionColRowByGoogle(region, i))
    }
    return result
}

/**
 * 天地图
 */
fun getRegionMAllLvTianDi(region: List<Point>, zoom: Int): MutableList<ArrayList<Int>> {
    val result = mutableListOf<ArrayList<Int>>()
    for (i in 1..zoom) {
        result.add(getRegionColRowTianDi(region, i))
    }
    return result
}

fun getRegionMAllLv(region: List<Point>, zoom: Int): MutableList<ArrayList<Int>> {
    val result = mutableListOf<ArrayList<Int>>()
    for (i in 0..zoom) {
        result.add(getRegionColRow(region, i))
    }
    return result
}

fun getMapUrlGoogle(level: Int, col: Int, row: Int):String {
    var iResult: String? = null
    var iRandom: Random? = null
    iResult = "http://mt"
    iRandom = Random()
    iResult += iRandom.nextInt(4)
    iResult = "$iResult.google.cn/vt/lyrs=s@110&hl=zh-CN&x=$col&y=$row&z=$level&s==Galil"

    return iResult
}

fun getMapUrl(level: Int, col: Int, row: Int): String {
    var iResult: String? = null
    var iRandom: Random? = null
    iResult = "http://t"
    iRandom = Random()
    iResult += iRandom.nextInt(8)
    iResult = "$iResult.tianditu.gov.cn/DataServer?T=img_c&x=$col&y=$row&l=$level&tk=e690e7a5cb4a3a5159e19fb8f823be72"
    return iResult
}

fun getForestMapUrl(level: Int, col: Int, row: Int, urlId: String):String {
    return if ("天地图" == urlId) {
        getMapUrl(level, col, row)
    } else {
        "http://10.254.253.2:82/${urlId}/${level}/${col}/${row}/tile.png"
    }
}

fun colRowNum(region: MutableList<ArrayList<Int>>): Long {
    var num = 0L

    region.forEach {
        num += (it[3] - it[1] + 1) * (it[2] - it[0] + 1)
    }

    return num
}

fun districtBoundaryList(districtBoundary : ArrayList<String>): MutableList<Point> {
    val pointList: MutableList<Point> = arrayListOf()
    for (result in districtBoundary) {
        for (point in result.split(";".toRegex()).toTypedArray()) {
            val pointss = point.split(",".toRegex()).toTypedArray()
            val x = pointss[0]
            val y = pointss[1]
            if (x == "NAN" || x == "" || y == "NAN" || y == "") {
                continue
            }
            val p = Point(x.toDouble(), y.toDouble())
            pointList.add(p)
        }
    }

    return pointList
}

val tianDiTuImagesSize = 13.5

fun estimateSpaceKb(number: Int) : Long{
    return (number * tianDiTuImagesSize).toLong()
}

fun estimateSpace(number:Int): String {
    var k = number * tianDiTuImagesSize

    if (k < 1024) {
        return "${k}K"
    }

    k /= 1024

    if (k < 1024) {
        return "${String.format("%.2f", k)}M"
    }

    k /= 1024

    return  return "${String.format("%.2f", k)}G"
}